
package com.android.formula.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Base for ListView adapter
 */
public abstract class TBaseAdapter<T> extends BaseAdapter {

    /******************************************** Variables ***********************************************/
    /**
     * The context which will show list item.
     */
    protected Context mContext;

    /**
     * Data of items.
     */
    protected List<T> mItems;

    /**
     * Layout inflater from context.
     */
    protected LayoutInflater mInflater;

    /**
     * Temporary item
     */
    protected T mTmpCurrentItem;

    /********************************************** Constructor *******************************************/
    /**
     * Create list adapter.
     * 
     * @param context The context which will show list item.
     * @param items List data.
     */
    public TBaseAdapter(final Context context, final List<T> items) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
        mContext = context;
        mItems = items;
    }

    /********************************************* Override *********************************************/
    @Override
    public int getCount() {
        if (mItems != null) {
            return mItems.size();
        } else {
            return 0;
        }
    }

    @Override
    public T getItem(int pos) {
        if (mItems == null || mItems.size() == 0 || pos < 0 || pos >= mItems.size()) {
            return null;
        }
        return mItems.get(pos);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = getView(view);
        }
        mTmpCurrentItem = getItem(i);
        if (mTmpCurrentItem != null) {
            setViews(view, mTmpCurrentItem);
        }
        return view;
    }

    /****************************************** Useful Methods *******************************************/
    /**
     * get ViewHolder from view
     * 
     * @param convertView
     * @return
     */
    protected abstract View getView(View convertView);

    /**
     * set data for Views
     * 
     * @param view
     * @param item
     * @return
     */
    protected abstract void setViews(View view, T item);

    /**
     * It will be implemented at inheritance class of this class.
     */
    public abstract void freeMemoryView(View v);

    /**
     * Base ViewHolder
     */
    public interface BaseViewHolder {
        void init(View convertView);
    }

    /**
     * Set Text For TextView
     * 
     * @param tv
     * @param text
     * @param isGone
     */
    protected void setText(TextView tv, String text, boolean isGone) {
        if (!TextUtils.isEmpty(text)) {
            tv.setText(text);
        } else if (isGone) {
            tv.setVisibility(View.GONE);
        } else {
            tv.setVisibility(View.INVISIBLE);
        }
    }
}
